<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('CapTable') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('变更日期')" prop="changeDate">
        <DatePicker
          v-model="changeDateArray"
          type="daterange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:equityChange:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
        <el-button
          type="primary"
          icon=""
          size="mini"
          @click="openCapTable"
        >CapTable</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealEquityChangeList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('变更日期')" align="center" prop="changeDate" width="180">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ parseTime(scope.row.changeDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资轮次')" align="center" prop="rounds" />
      <el-table-column :label="$t('融资方式')" align="center" prop="roundsType" :formatter="formatterRoundsType" />
      <el-table-column :label="$t('融资前公司估值') + $t('金额单位')" align="center" prop="beforeValuation" width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.beforeValuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资前-公司总股数(万股)/注册资本(万)')" align="center" prop="beforeShareNum">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.beforeShareNum, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资后每股价格') + $t('(元)')" align="center" prop="afterSharePrice" width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.afterSharePrice, '') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:equityChange:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:equityChange:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目股权变更主（capTable）对话框 -->
    <el-dialog title="CapTable" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" top="7vh" custom-class="dialogFormEquityChange" append-to-body>
      <dealEquityChangeForm ref="dealEquityChangeForm" />
    </el-dialog>
    <!-- capTable对话框 -->
    <el-dialog title="CapTable" :visible.sync="capTableFormOpen" :close-on-click-modal="false" :lock-scroll="false" top="7vh" custom-class="dialogForm" append-to-body>
      <capTableReport ref="capTableReport" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealEquityChange, delDealEquityChange } from '@/api/deal/equitychange/dealEquityChange'
import dealEquityChangeForm from './dealEquityChangeForm.vue'
import capTableReport from './capTableReport.vue'
import moment from 'moment'
export default {
  components: {
      dealEquityChangeForm,
      capTableReport
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 项目股权变更主（capTable）表格数据
          dealEquityChangeList: [],
          // 弹出层标题
          title: 'CapTable',
          // 是否显示弹出层
          formOpen: false,
          // capTable 弹出层
          capTableFormOpen: false,
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              changeDate: undefined,
              changeStartDate: undefined,
              changeEndDate: undefined,
              rounds: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 查询参数 时间数组
          changeDateArray: [],
          // 表单校验
          rules: {
          },
          roundsTypeOption: []
      }
  },
  created() {
    this.getDicts('financing_mode').then(response => {
      this.roundsTypeOption = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询项目股权变更主（CapTable）列表 */
    getList() {
        this.loading = true
        if (this.changeDateArray !== undefined) {
          if (this.changeDateArray[0] !== null && this.changeDateArray[0] !== undefined) {
            this.queryParams.changeStartDate = moment(this.changeDateArray[0]).format('YYYY-MM-DD')
          }
          if (this.changeDateArray[1] !== null && this.changeDateArray[1] !== undefined) {
            this.queryParams.changeEndDate = moment(this.changeDateArray[1]).format('YYYY-MM-DD')
          }
        }
        listDealEquityChange(this.queryParams).then(response => {
            this.dealEquityChangeList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: this.$route.query.id,
            changeDate: undefined,
            rounds: undefined,
            enterpriseStage: undefined,
            beforeValuation: undefined,
            beforeShareNum: undefined,
            afterSharePrice: undefined,
            termsId: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    formatterRoundsType(row) {
      return this.selectDictLabel(this.roundsTypeOption, row.roundsType)
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.changeDateArray = []
        this.queryParams.changeStartDate = undefined
        this.queryParams.changeEndDate = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealEquityChangeForm.init(null)
        })
    },
    /** capTable */
    openCapTable() {
        this.capTableFormOpen = true
        this.$refs.capTableReport.loading = true
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealEquityChangeForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealEquityChange(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    }
  }
}
</script>
